<template>
  <div>
    <el-table
      :data="cartItemList"
      :stripe="true"
      style="width: 100%">
      <el-table-column
        label="零食名">
        <template slot-scope="scope">
          {{scope.row.food.name}}
        </template>
      </el-table-column>
      <el-table-column
        label="零食价格">
        <template slot-scope="scope">
          {{scope.row.food.price}}
        </template>
      </el-table-column>

      <el-table-column
        prop="count"
        label="数量">
      </el-table-column>

      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button @click="deleteCartItem(scope.row.food.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-row :gutter="10" v-if="cartItemList.length>0">
     <el-col :span="18">总金额：{{cart.totalMoney}}元</el-col>
     <el-col :span="6"><el-button @click="genarateOrder()">生成订单</el-button></el-col>
    </el-row>
  </div>
</template>

<script>
    export default {
        name: "cart",
      data(){
          return {
            cart:{},
            cartItemList:[]
          }
      },
      created(){
        this.selectMyCart();
      },
      methods:{
        deleteCartItem(id){
          this.axios.delete("/cart/delete/"+id).then((resp)=>{
            this.selectMyCart();
          });
        },
        selectMyCart(){
          //发出ajax请求请求后端代码
          this.axios.get("/cart/myCart").then((resp)=>{
            this.cart=resp.data;
            this.cartItemList=[];
            for(let i in this.cart.buyMap){
              this.cartItemList.push(this.cart.buyMap[i]);
            }
          });
        },
        genarateOrder(){
          this.axios.post("/order/genateOrder").then((resp)=>{
            this.$message.success("订单生成成功！");
            this.$router.push("/order/OrderList");
          });
        }
      }
    }
</script>

<style scoped>

</style>
